home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / BTDELETE.C < prev    next >
Text File  |  1991-09-23  |  2KB  |  96 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)btdelete.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10.  
  11. /* library headers */
  12. #include <blkio.h>
  13.  
  14. /* local headers */
  15. #include "btree_.h"
  16.  
  17. /*man---------------------------------------------------------------------------
  18. NAME
  19.      btdelete - delete btree key
  20.  
  21. SYNOPSIS
  22.      #include <btree.h>
  23.  
  24.      int btdelete(btp, buf)
  25.      btree_t *btp;
  26.      const void *buf;
  27.  
  28. DESCRIPTION
  29.      The btdelete function searches for the key in btree btp with the
  30.      value pointed to by buf and deletes it.  The cursor is positioned
  31.      to null.
  32.  
  33.      btdelete will fail if one or more of the following is true:
  34.  
  35.      [EINVAL]       btp is not a valid btree pointer.
  36.      [EINVAL]       buf is the NULL pointer.
  37.      [BTELOCK]      btp is not write locked.
  38.      [BTENKEY]      The key pointed to by buf is not in btp.
  39.      [BTENOPEN]     btp is not open.
  40.  
  41. SEE ALSO
  42.      btdelcur, btinsert, btsearch.
  43.  
  44. DIAGNOSTICS
  45.      Upon successful completion, a value of 0 is returned.  Otherwise,
  46.      a value of -1 is returned, and errno set to indicate the error.
  47.  
  48. ------------------------------------------------------------------------------*/
  49. #ifdef AC_PROTO
  50. int btdelete(btree_t *btp, const void *buf)
  51. #else
  52. int btdelete(btp, buf)
  53. btree_t *btp;
  54. const void *buf;
  55. #endif
  56. {
  57.     int found = 0;
  58.  
  59.     /* validate arguments */
  60.     if (!bt_valid(btp) || buf == NULL) {
  61.         errno = EINVAL;
  62.         return -1;
  63.     }
  64.  
  65.     /* check if not open */
  66.     if (!(btp->flags & BTOPEN)) {
  67.         errno = BTENOPEN;
  68.         return -1;
  69.     }
  70.  
  71.     /* check if not write locked */
  72.     if (!(btp->flags & BTWRLCK)) {
  73.         errno = BTELOCK;
  74.         return -1;
  75.     }
  76.  
  77.     /* make buf the current key and generate search path */
  78.     found = btsearch(btp, buf);
  79.     if (found == -1) {
  80.         BTEPRINT;
  81.         return -1;
  82.     }
  83.     if (found == 0) {        /* key not in btree btp */
  84.         errno = BTENKEY;
  85.         return -1;
  86.     }
  87.  
  88.     /* delete the current key */
  89.     if (bt_delete(btp) == -1) {
  90.         BTEPRINT;
  91.         return -1;
  92.     }
  93.  
  94.     return 0;
  95. }
  96.